

class window.Font437
  constructor : (ctx) ->
    @ready = false
    @fontTex = ctx.Texture()
    me = @

    @spr = ctx.SpriteRender()

    @promise = @fontTex.load("img/font.png").then () -> 
      me.ready = true
      me.spr.setSpriteSheet me.fontTex, 8, 8

    @worldTransform = new window.Transform2D()
    @localTransform = new window.Transform2D()
    @spr.setWorldTransform @worldTransform

  use : () ->
    @spr.use()

  drawOutlined : (x, y, string, scale, r, g, b, a) ->
    @setColor 0.0, 0.0, 0.0, 1.0
    @drawString x + scale, y, string, scale

    @setColor r, g, b, a
    @drawString x, y, string, scale

  setColor : (x,y,z,w) -> @spr.setColor x,y,z,w

  drawString : (x, y, string, scale) ->
    return false if not @ready
    scale ?= 1.0
    @localTransform.setScale scale
    @localTransform.setTranslation x, y

    for x in [0..string.length - 1]
      charCode = string.charCodeAt x
      column = charCode >> 5
      row = charCode - (column << 5)

      @spr.drawSprite [row * 8, column * 8], @localTransform
      @localTransform.translate 8 * scale, 0

    true